package com.biannuo.szk.community.web;

import com.biannuo.szk.common.base.BaseResource;
import com.biannuo.szk.common.util.SecurityUtils;
import com.biannuo.szk.common.vo.ResponseVO;
import com.biannuo.szk.community.service.IBlogService;
import com.biannuo.szk.community.service.dto.BlogDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Pageable;

@Slf4j
@Api(tags = "博客管理，针对我的博客管理功能")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/blog")
public class BlogController extends BaseResource {

    @Autowired
    private final IBlogService blogService;

    @ApiOperation("发布博客")
    @PostMapping("/save")
    public ResponseVO saveBlog(@RequestBody BlogDTO blogWriteDTO) {
        Long currentUserId = SecurityUtils.getCurrentUserId();
        blogWriteDTO.setUserId(currentUserId);
        blogService.saveBlog(blogWriteDTO);
        return buildSuccess("success");
    }

    @ApiOperation(value = "删除我的原创博客")
    @PostMapping("/remove")
    public ResponseEntity removeBlog(@RequestParam Long id) {
        blogService.removeBlog(id, SecurityUtils.getCurrentUserId());
        return ResponseEntity.ok().build();
    }

    @ApiOperation("更新博客")
    @PostMapping("/update")
    @Deprecated
    public ResponseEntity updateBlog(@RequestBody BlogDTO blogDTO) {
        Long currentUserId = SecurityUtils.getCurrentUserId();
        blogDTO.setUserId(currentUserId);
        blogService.updateBlog(blogDTO);
        return ResponseEntity.ok().build();
    }

    @ApiOperation(value = "获取博客列表")
    @GetMapping(value = "/list")
    @Deprecated
    public ResponseEntity listBlog(Pageable pageable, @RequestParam("title") String title) {
//        IPage<Object> page = Condition.getPage(pageable);
//        IPage<BlogDTO> iPage = blogService.listBlog(page,title);
//        return ResponseEntity.ok(iPage);
        return null;
    }
}
